using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Authorization;
using GoodDotNet.WebAdmin.Controllers;
using GoodDotNet.Core.Model;
using GoodDotNet.Service;
using GoodDotNet.Core.Entity;
using GoodDotNet.WebAdmin.HttpContextUser;
using Microsoft.Extensions.Options;
using GoodDotNet.Data;
using GoodDotNet.Core.Model;
using GoodDotNet.Core.Data;

namespace GoodDotNet.Webadmin.Controllers
{
    [ApiController]
	[Route("api/admin-role-module")]
	public class AdminRoleModuleController:BaseController
	{
        public AdminRoleModuleController(IUser user, IHttpContextAccessor accessor, IOptions<List<ConnectionString>> conns, ILogger<BaseController> logger, IOptions<Appsettings> appsettings) : base(user, accessor, conns, logger, appsettings)
        {
        }

        [HttpPost]
        [Route("ListByRole")]
		public Result ListByRole([FromBody]PostID par)
        {
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                 AdminRoleModuleService service = new AdminRoleModuleService(uniOfWork);
                var list= service.GetListByRole(par.ID);
                return Sucess(list);
            }
           
        }
        [HttpPost]
        [Authorize]
        [Route("ListByModule")]
        public Result ListByModule([FromBody] PostID par)
        {
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminRoleModuleService service = new AdminRoleModuleService(uniOfWork);
                var list = service.GetListByModule(par.ID);
                return Sucess(list);
            }

        }
        [HttpPost]
        [Route("updataRolePermission")]
        [Authorize]
        public Result updataRolePermission([FromBody] ParUpdateRolePermission pars )
        {
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminModuleService service = new AdminModuleService(uniOfWork);
                service.UpdataRolePermission(pars.RoleId,pars.modules,_user.Id);
                return Sucess("success");
            }

        }
        [HttpPost]
        [Route("updataModulePermission")]
        [Authorize]
        public Result updataModulePermission([FromBody] ParUpdateModulePermission pars)
        {
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminModuleService service = new AdminModuleService(uniOfWork);
                service.UpdataModulePermission(pars.ModuleId, pars.Roles, _user.Id);
                return Sucess("success");
            }

        }
       
        [HttpPost]
        [Authorize]
        [Route("delete")]
        public Result Delete([FromBody] PostID pars)
        {
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminRoleModule entity = uniOfWork.AdminRoleModuleRepository.Get(a => a.Id == pars.ID);
                if(entity == null)
                {
                    return Error("找不到记录");
                }
                uniOfWork.AdminRoleModuleRepository.Remove(entity);
                return Sucess("sucess");
            }
        }


    
		
		[HttpPost]
		[Route("UpdateRoleModuleBtns")]
		[Authorize]
		public Result  UpdateRoleModuleBtns([FromBody] AdminRoleModuleUpdateRoleModuleBtnsParModel pars)
		{
		   
		        using (var uniOfWork = new UnitOfWork(_conStr))
		        {
		           AdminRoleModuleService service = new AdminRoleModuleService(uniOfWork);
		                service.UpdateRoleModuleBtns(pars);
		        }
		            return Sucess("sucess");
		  
		
		}
		
}
}
